/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.insta.browser.view.menu;

import fr.insta.browser.domain.IHistoryEntry;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 * <p>HistoryPopup class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public class HistoryPopup extends JPopupMenu {

    public interface HistoryPopupDelegate {

        /**
         *
         * @param aThis
         * @param entry
         * @param item
         * @param e
         */
        public void onClickItem(HistoryPopup aThis, IHistoryEntry entry, JMenuItem item, ActionEvent e);
    }

    /**
     * <p>Constructor for HistoryPopup.</p>
     *
     * @param entries a {@link java.util.Iterator} object.
     * @param delegate a {@link fr.insta.browser.view.menu.HistoryPopup.HistoryPopupDelegate} object.
     */
    public HistoryPopup(Iterator<IHistoryEntry> entries, final HistoryPopupDelegate delegate) {
        super();

        while (entries.hasNext()) {
            final IHistoryEntry entry = entries.next();
            final String label = !"".equals(entry.getTitle()) ? entry.getTitle() : entry.getUrl();
            final JMenuItem item = new JMenuItem(label);

            item.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    delegate.onClickItem(HistoryPopup.this, entry, item, e);
                }
            });
            add(item);
        }
    }
}
